package cn.admobiletop.adsuyi.adapter.gdt.listener;

import com.qq.e.ads.interstitial2.UnifiedInterstitialAD;
import com.qq.e.ads.interstitial2.UnifiedInterstitialADListener;
import com.qq.e.comm.util.AdError;

import cn.admobiletop.adsuyi.ad.listener.ADSuyiInterstitialAdListener;
import cn.admobiletop.adsuyi.adapter.gdt.data.InterstitialAdInfo;

/**
 * @author ciba
 * @description 插屏广告监听
 *              需要继承 BaseAdListener<ADSuyiInterstitialAdListener>，实现所适配渠道的开屏广告监听回调，并在回调中调用事先set的adListener，进行相应回调
 *              需要在渠道回调中调用ADSuyi以下方法：
 *                  getAdListener().onAdReceive(this)     广告获取
 *                  getAdListener().onVideoCache(this)    广告已缓存
 *                  getAdListener().onAdExpose(this)      广告曝光
 *                  getAdListener().onAdClose(this)       广告关闭
 *                  getAdListener().onAdClick(this)       广告点击
 *                  onAdFailed(code, message)             广告失败
 * @date 2020/4/8
 */
public class InterstitialAdListener extends BaseAdListener<ADSuyiInterstitialAdListener> implements UnifiedInterstitialADListener {
    private UnifiedInterstitialAD unifiedInterstitialAD;
    private InterstitialAdInfo interstitialAdInfo;

    public InterstitialAdListener(String platformPosId, ADSuyiInterstitialAdListener adListener) {
        super(platformPosId, adListener);
    }

    public void setUnifiedInterstitialAD(UnifiedInterstitialAD unifiedInterstitialAD) {
        this.unifiedInterstitialAD = unifiedInterstitialAD;
    }

    /**
     * 广点通广告获取成功
     */
    @Override
    public void onADReceive() {
        if (getAdListener() != null && unifiedInterstitialAD != null) {
            interstitialAdInfo = new InterstitialAdInfo(getPlatformPosId());
            interstitialAdInfo.setAdapterAdInfo(unifiedInterstitialAD);

            getAdListener().onAdReceive(interstitialAdInfo);
            getAdListener().onAdReady(interstitialAdInfo);
        }
    }

    /**
     * 广点通视频素材缓存成功
     */
    @Override
    public void onVideoCached() {}

    /**
     * 广点通广告加载失败
     * @param adError
     */
    @Override
    public void onNoAD(AdError adError) {
        onAdFailed(adError.getErrorCode(), adError.getErrorMsg());
    }

    /**
     * 广点通广告展开
     */
    @Override
    public void onADOpened() {}

    /**
     * 广点通广告曝光
     */
    @Override
    public void onADExposure() {
        if (getAdListener() != null && interstitialAdInfo != null) {
            getAdListener().onAdExpose(interstitialAdInfo);
        }
    }

    /**
     * 广点通广告被点击
     */
    @Override
    public void onADClicked() {
        if (getAdListener() != null && interstitialAdInfo != null) {
            getAdListener().onAdClick(interstitialAdInfo);
        }
    }

    /**
     * 广点通广告点击离开应用时回调
     */
    @Override
    public void onADLeftApplication() {}

    /**
     * 广点通广告被关闭
     */
    @Override
    public void onADClosed() {
        if (getAdListener() != null && interstitialAdInfo != null) {
            // 如果发生视频播放错误等问题导致关闭走onADClose回调
            getAdListener().onAdClose(interstitialAdInfo);
        }
    }

    /**
     * 广点通广告渲染成功时回调
     */
    @Override
    public void onRenderSuccess() {}

    /**
     * 广点通广告渲染失败时回调
     */
    @Override
    public void onRenderFail() {}

    /**
     * ADSuyi广告释放会调用该方法
     */
    @Override
    public void release() {
        super.release();
        unifiedInterstitialAD = null;
        if (interstitialAdInfo != null) {
            interstitialAdInfo.release();
            interstitialAdInfo = null;
        }
    }
}
